﻿package com.projects.xenia;

import java.util.Calendar;

import com.projects.xenia.global_modules.MainGlobalModule;


import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import android.widget.BaseAdapter;
import android.widget.TextView;


public class Activity_PlanOfBrandSales extends ListActivity {
	
	private EfficientAdapter adap;
	private Cursor PlanCursor;
	
	private TextView pbs_descr1;
	private TextView pbs_descr2;
	private TextView pbs_descr3;
	
	private String Brand_Descr;
	private Double Brand_Plan_Value;
	private Double Brand_Fact_Value;
	private Double Brand_DayFact_Value;
	
	private int Days_InMonth;
	private int Days_Past;
	private int Days_Rest;
	
	private void GetCursor () {
		
		try {
			PlanCursor = MainGlobalModule.DBE.rawQuery("select b.descr, p.plan_value, p.fact_value, p.day_fact_value from xref_PlanOfBrandSales p inner join xref_brands b on p.brands_id = b.id", null);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	private void GetCursorData (int position) {

		PlanCursor.moveToPosition(position);
		Brand_Descr = PlanCursor.getString(0);
		Brand_Plan_Value = PlanCursor.getDouble(1);		
		Brand_DayFact_Value = PlanCursor.getDouble(3);
		Brand_Fact_Value = Brand_DayFact_Value + PlanCursor.getDouble(2);
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
	
    requestWindowFeature(Window.FEATURE_NO_TITLE);            
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	setContentView(R.layout.planofbrandsales_form);
	
	
	Days_InMonth = 0;
	Days_Past = 0;
	Days_Rest = 0;
	
	 
	Calendar CurrCalendar = Calendar.getInstance();
	int currday = CurrCalendar.get(Calendar.DAY_OF_MONTH); 
	CurrCalendar.add(Calendar.DAY_OF_MONTH, 1 - CurrCalendar.get(Calendar.DAY_OF_MONTH));
	Calendar StopCalendar = (Calendar)CurrCalendar.clone();
	StopCalendar.add(Calendar.MONTH,1);
	
	
	String CC1 = CurrCalendar.getTime().toString(); 
	String CC2 = StopCalendar.getTime().toString();
	
	int DaysInWeek = Integer.parseInt(MainGlobalModule.ConstantsCollection.get("DaysOfWorkWeek")); 
	
	if (DaysInWeek==6) {
		while (CurrCalendar.get(Calendar.DAY_OF_YEAR) < StopCalendar.get(Calendar.DAY_OF_YEAR)) {
			if (CurrCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
				Days_InMonth ++;
				if (currday > CurrCalendar.get(Calendar.DAY_OF_MONTH)) {
					Days_Past ++;
				}
			}
			CurrCalendar.add(Calendar.DAY_OF_MONTH, 1);
		}		
	}
	else
	{
		while (CurrCalendar.get(Calendar.DAY_OF_YEAR) < StopCalendar.get(Calendar.DAY_OF_YEAR)) {
			if ((CurrCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) & CurrCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY ) {
				Days_InMonth ++;
				if (currday > CurrCalendar.get(Calendar.DAY_OF_MONTH)) {
					Days_Past ++;
				}
			}
			CurrCalendar.add(Calendar.DAY_OF_MONTH, 1);
		}		
	}
	
	
	Days_Past++;
	
	Days_Rest = Days_InMonth - Days_Past;
	
	pbs_descr1 = (TextView)findViewById(R.id.pbs_descr1);
	pbs_descr1.setText(String.format("%tb-%tY", Calendar.getInstance().getTime(),Calendar.getInstance().getTime()));
	
	pbs_descr2 = (TextView)findViewById(R.id.pbs_descr2);
	pbs_descr2.setText(String.format("Рабочие дни - %d из %d, ост. %d",Days_Past,Days_InMonth,Days_Rest));
	
	pbs_descr3 = (TextView)findViewById(R.id.pbs_descr3);
	pbs_descr3.setText(String.format("%s",""));
	
	GetCursor ();
	
	adap = new EfficientAdapter(this);
	setListAdapter(adap);	

	}
	
	public class EfficientAdapter extends BaseAdapter  {
		
		private LayoutInflater mInflater;
		public Context context;
		 
		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}
		 
		public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		
		if (convertView == null) {			
			convertView = mInflater.inflate(R.layout.planofbrandsales_list, null);
		}
		
		GetCursorData (position);
		
		holder = new ViewHolder();
		
		
		
		//// brand
		holder.pbsl_brand_descr = (TextView)convertView.findViewById(R.id.pbsl_brand_descr);
		holder.pbsl_brand_descr.setText(Brand_Descr);
		
		//// plan
		holder.pbsl_plan_total = (TextView)convertView.findViewById(R.id.pbsl_plan_total);
		holder.pbsl_plan_total.setText(String.format ("%.2f",Brand_Plan_Value));
		
		holder.pbsl_plan_day = (TextView)convertView.findViewById(R.id.pbsl_plan_day);
		holder.pbsl_plan_day.setText(String.format ("%.2f",Brand_Plan_Value/Days_InMonth));
		
		holder.pbsl_plan_percent = (TextView)convertView.findViewById(R.id.pbsl_plan_percent);
		holder.pbsl_plan_percent.setText(String.format ("100"));
		
				
		Double predictionSale = ((Brand_Fact_Value/Days_Past)*Days_InMonth);
		
		//// fact
		holder.pbsl_fact_total =  (TextView)convertView.findViewById(R.id.pbsl_fact_total);
		holder.pbsl_fact_total.setText(String.format ("%.2f",Brand_Fact_Value));
		
		holder.pbsl_fact_day =  (TextView)convertView.findViewById(R.id.pbsl_fact_day);
		holder.pbsl_fact_day.setText(String.format ("%.2f",Brand_DayFact_Value));
		
		holder.pbsl_fact_percent =  (TextView)convertView.findViewById(R.id.pbsl_fact_percent);
		holder.pbsl_fact_percent.setText(String.format ("%.2f",((Brand_Fact_Value)/Brand_Plan_Value)*100));		
		
		/// prediction		
		holder.pbsl_prediction_total = (TextView)convertView.findViewById(R.id.pbsl_prediction_total);
		holder.pbsl_prediction_total.setText(String.format ("%.2f",predictionSale));
		
		holder.pbsl_prediction_day = (TextView)convertView.findViewById(R.id.pbsl_prediction_day);
		holder.pbsl_prediction_day.setText(String.format ("%.2f",(predictionSale/Days_InMonth)));
		
		holder.pbsl_prediction_percent = (TextView)convertView.findViewById(R.id.pbsl_prediction_percent);
		holder.pbsl_prediction_percent.setText(String.format ("%.2f", (predictionSale/Brand_Plan_Value)*100));
		
		if (((predictionSale/Brand_Plan_Value)*100)<100) {
			holder.pbsl_prediction_percent.setTextColor(Color.RED);
		}
		else
		{
			holder.pbsl_prediction_percent.setTextColor(Color.BLUE);
		}
		
		/// target
		holder.pbsl_target_total = (TextView)convertView.findViewById(R.id.pbsl_target_total);
		holder.pbsl_target_total.setText(String.format ("%.2f",Brand_Plan_Value-Brand_Fact_Value));		
		holder.pbsl_target_day = (TextView)convertView.findViewById(R.id.pbsl_target_day);
		holder.pbsl_target_day.setText(String.format ("%.2f",(Brand_Plan_Value-Brand_Fact_Value)/Days_Rest));
				
		return convertView;
		
		}
		 
		class ViewHolder {		
			
			TextView pbsl_brand_descr;
			
			TextView pbsl_plan_total;
			TextView pbsl_plan_day;
			TextView pbsl_plan_percent;
			
			TextView pbsl_fact_total;
			TextView pbsl_fact_day;
			TextView pbsl_fact_percent;
			
			TextView pbsl_prediction_total;
			TextView pbsl_prediction_day;
			TextView pbsl_prediction_percent;
			
			TextView pbsl_target_total;
			TextView pbsl_target_day;
			TextView pbsl_target_percent;
			
		}
		 
				 
		@Override
		public long getItemId(int position) {
			
			return position;
			
		}
		 
		@Override
		public int getCount() {

			return PlanCursor.getCount();
		}
		 
		@Override
		public Object getItem(int position) {
			
			GetCursorData (position);
			
			return position;
		
		}	
	}

	
}
